#!/usr/bin/env groovy

/**
* This shared lib depends on emailExt plugin
* Usage:
    post { //after build
        changed { //if status changed then send email
            sendEmail('dest1@mail.com, dest2@mail.com')
        }
    }
 *
 */
def call(String recipients) {
    stage('Send email') {
    
    }
        def status, logRegex
        switch (currentBuild.currentResult) {
            case 'SUCCESS':
                status = 'Successed'
                statusBody = '<h1 style="color:Green">Successed</h1>'
                logRegex = 'SUCCESS'
                break

            case 'UNSTABLE':
                status = 'Unstable'
                statusBody = '<h1 style="color:Red">Unstable</h1>'
                logRegex = 'FAILURE'
                break

            case 'FAILURE':
                status = 'Failed'
                statusBody = '<h1 style="color:Red">Failed</h1>'
                logRegex = 'FAILURE'
                break

            case 'ABORTED':
                status = 'Canceled'
                statusBody = '<h1 style="color:Red">Canceled</h1>'
                logRegex = 'ABORTED'
                break

        }
        emailext(subject: "Build $status - ${JOB_NAME} #${BUILD_NUMBER} ",
                body: """ <p> Build $statusBody on Job:
                <a style = "font-size:14px;text-decoration:underline;font-weight:bold" href="${BUILD_URL}/console">${
                    JOB_NAME
                } - build# ${BUILD_NUMBER} </a></p>
                <p> <pre> \${BUILD_LOG_REGEX, regex = "^.*?$logRegex.*?\$", linesBefore = 25, linesAfter = 150, maxMatches = 10, showTruncatedLines = false, escapeHtml = false} </pre></p> """
                , mimeType: 'text/html'
                //, from: '"Jenkins server" <foo@acme.org>'
                , to: "$recipients")

}
